<?php

namespace App\Admin\Controllers;

use App\Models\UserHealth;
use App\Models\User;
use Carbon\Carbon;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class UserHealthController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new UserHealth(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('user_id');
            $grid->column('blood_pressure');
            $grid->column('blood sugar');
            $grid->column('uric_acid');
            $grid->column('bmi');
            $grid->column('fat');
            $grid->column('med_history');
            $grid->column('past_med');
            $grid->column('diagnosis');
            $grid->column('summary');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserHealth(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('blood_pressure');
            $show->field('blood sugar');
            $show->field('uric_acid');
            $show->field('bmi');
            $show->field('fat');
            $show->field('med_history');
            $show->field('past_med');
            $show->field('diagnosis');
            $show->field('summary');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserHealth(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('blood_pressure');
            $form->text('blood sugar');
            $form->text('uric_acid');
            $form->text('bmi');
            $form->text('fat');
            $form->text('med_history');
            $form->text('past_med');
            $form->text('diagnosis');
            $form->text('summary');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    public function userHealth(Content $content, Request $request)
    {
        $userId = $request->input('user_id');
        $user = User::find($userId);
        $age = '';
        if (!empty($user->birthday)) {
            $birthday = Carbon::createFromDate($user->birthday);
            $today = Carbon::now();
            $age = $today->diffInYears($birthday);
        }

        $form = UserHealth::where('user_id', $userId)
            ->get()->first();

        $content->title('体态评估表');
        $content->description('');
        return $content->body(admin_view('health_form', compact('age', 'user', 'userId', 'form')));
    }

    /**
     * @param Content $content
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function addUserHealth(Content $content, Request $request)
    {
        $userId = $request->input('user_id');
        $user = User::find($userId);

        $formId = $request->input('form_id');
        if (!empty($formId)) {
            $form = UserHealth::find($formId);
        } else {
            $form = new UserHealth();
        }

        DB::beginTransaction();
        try {

            $form->user_id = $userId;
            $form->blood_pressure = $request->input('blood_pressure');
            $form->blood_sugar = $request->input('blood_sugar');
            $form->uric_acid = $request->input('uric_acid');
            $form->bmi = $request->input('bmi');
            $form->fat = $request->input('fat');
            $form->med_history = $request->input('med_history');
            $form->past_med = $request->input('past_med');
            $form->diagnosis = $request->input('diagnosis');
            $form->summary = $request->input('summary');
            $form->save();
            DB::commit();

            $content->withSuccess('更新成功');
            return redirect()->back();

        } catch (\Exception $e) {
            DB::rollBack();
            $content->withError($e->getMessage());
            return redirect()->back();
        }
    }
}
